package com.atguigu.guli.service.edu.controller.api;


import com.atguigu.guli.service.base.model.dto.CourseDto;
import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.base.result.ResultCodeEnum;
import com.atguigu.guli.service.edu.entity.Course;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.query.ApiCourseQuery;
import com.atguigu.guli.service.edu.entity.vo.ApiCourseDetailVo;
import com.atguigu.guli.service.edu.service.CourseService;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-05-18
 */
@RestController

@RequestMapping("/api/edu/course")
public class ApiCourseController {

    @Autowired
    CourseService courseService;
    @Autowired
    TeacherService teacherService;
    @Autowired
    RedisTemplate redisTemplate;
    //5、更新课程销量 : 可能会有重复访问  为了保证幂等性，可以通过uuid的方式来校验请求
    @PutMapping("updateSales/{id}")
    public R updateSales(@PathVariable String id , @RequestParam String uuid){
        Boolean hasKey = redisTemplate.hasKey(uuid);
        if(hasKey){
            return R.setResultCodeEnum(ResultCodeEnum.UNKNOWN_REASON);
        }
        //uuid 第一次来访问，处理请求 并将uuid作为键保存到redis中1分钟
        redisTemplate.opsForValue().set(uuid , "1" , 1 , TimeUnit.MINUTES);
        Course course = courseService.getById(id);
        course.setBuyCount(course.getBuyCount()+1);
        courseService.updateById(course);
        return R.ok();
    }
    //4、创建订单时获取课程信息
    @GetMapping("get-courseinfo/{id}")
    public R getCourseInfo(@PathVariable String id){
        Course course = courseService.getById(id);
        Teacher teacher = teacherService.getById(course.getTeacherId());
        CourseDto courseDto = new CourseDto();
        BeanUtils.copyProperties(course , courseDto);
        courseDto.setTeacherName(teacher.getName());

        return R.ok().setData("item" , courseDto);
    }

    //3、根据课程id查询课程详情
    @GetMapping("getById/{id}")
    public R getById(@PathVariable String id){
        ApiCourseDetailVo courseDetailVo  = courseService.getCourseDetailsById(id);
        return R.ok().setData("item" , courseDetailVo);
    }

    //2、根据筛选条件查询课程列表
    @GetMapping("list") //@RequestBody
    public R listCourses(ApiCourseQuery apiCourseQuery){
        List<Course> courses = courseService.listCourses(apiCourseQuery);
        return R.ok().setData("items" , courses);
    }
    //1、根据讲师id查询他的课程列表
    @GetMapping("getCourses/{teacherId}")
    public R getCourses(@PathVariable  String teacherId){
        LambdaQueryWrapper<Course> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Course::getTeacherId , teacherId);
        queryWrapper.select(Course::getId , Course::getCover , Course::getTitle);
        List<Course> courses = courseService.list(queryWrapper);
        return R.ok().setData("items" , courses);
    }

}

